package com.syni.mdd.yhd.common.all.service.impl.activity;

import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.activity.BmsBackCardDao;
import com.syni.mdd.yhd.common.all.dao.jpa.activity.BmsDxActivityDao;
import com.syni.mdd.yhd.common.all.entity.activity.BmsBackCard;
import com.syni.mdd.yhd.common.all.entity.activity.BmsDrLogic;
import com.syni.mdd.yhd.common.all.entity.activity.BmsDrOrderLog;
import com.syni.mdd.yhd.common.all.entity.activity.BmsDxActivity;
import com.syni.mdd.yhd.common.all.service.activity.BmsBackCardService;
import com.syni.mdd.yhd.common.all.service.activity.BmsDrLogicService;
import com.syni.mdd.yhd.common.all.service.activity.BmsDrOrderLogService;
import com.syni.mdd.yhd.common.all.service.activity.BmsDxActivityService;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.yhd.common.all.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @className
 * @description TOO
 * @Author hwq
 * @DATE 2019/9/23 10:34
 * @VERSION 1.0
 **/
@Service
public class BmsDxActivityServiceImpl extends BaseService<BmsDxActivity, Integer> implements BmsDxActivityService {

    @Autowired
    BmsDxActivityDao bmsDxActivityDao;

    @Autowired
    BmsDrLogicService bmsDrLogicService;

    @Override
    public BmsDxActivityDao getBaseDao() {
        return bmsDxActivityDao;
    }

    @Override
    public Map<String, Object> getActivitys(ParamsMap paramsMap) {
        Integer userId = null;
        Map<String,Object> checkRet = RequestParamUtils.checkAuthor(paramsMap);
        if(checkRet.containsKey("status") && ResultCode.CODE_NORMAL.equals(checkRet.get("status").toString())){
            userId = paramsMap.getInteger("userIdentify");
        }
        Integer timestamp = DateTimeUtils.getNowTimeStamp();
        Map<String,Object> conditionMap = new HashMap<>();
        //conditionMap.put("isOpen",Constants.COMMON_FLAG_TRUE_INT);
        List<BmsDxActivity> bmsDxActivities = bmsDxActivityDao.findAll(new SpecificationUtils(conditionMap));
        if(bmsDxActivities != null && bmsDxActivities.size() > 0){
            for(BmsDxActivity bmsDxActivity : bmsDxActivities){
                if(DateTimeUtils.converDateToInterger(bmsDxActivity.getEndTime()) <= timestamp){
                    if(userId != null) {
                        if (bmsDxActivity.getActivityName().equals("老板请客")) {
                            //判断用户是否开启了任务，未到结束时间
                            List<BmsDrLogic> bmsDrLogics = bmsDrLogicService.getUserTask(userId);
                            if(bmsDrLogics != null && bmsDrLogics.size() > 0){
                                BmsDrLogic bmsDrLogic = bmsDrLogics.get(0);
                                if(bmsDrLogic.getCompleteNum() < 5 && bmsDrLogic.getExpireTime() != null){
                                    if(DateTimeUtils.converDateToInterger(bmsDrLogic.getExpireTime()) > timestamp){
                                        bmsDxActivity.setIsOpen(Constants.COMMON_FLAG_TRUE_INT);
                                        continue;
                                    }
                                }
                            }

                        }
                    }
                    bmsDxActivity.setIsOpen(Constants.COMMON_FLAG_FALSE_INT);
                }
            }
            bmsDxActivityDao.saveAll(bmsDxActivities);
        }
        return ResultMapHelper.success(bmsDxActivities);
    }
}
